package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.DateControl;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSTransDAO;

public class DutyTimeSectionDAO {
	
	private IBaseDAO baseDAO;
	
	private ITransactionDAO tranDAO;
	

	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}

	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}

	/**
	 * @return the baseDAO
	 */
	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	/**
	 * @param baseDAO the baseDAO to set
	 */
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public List<Map<String, Object>> getDutyTimeRes(String thisWorkDay) {
		List<Map<String, Object>> list = null;
		String table = GlobalDBVar.getDBFullName_Conn("Msg_Timing_Pstn_Layering_"+thisWorkDay, "3", null);
		StringBuilder sql = new StringBuilder("select MsgTPL_Vcl_ID vclid,MsgTPL_FirstLongitude flo,MsgTPL_FirstLatitude fla,MsgTPL_BeginTime ftime,");
		sql.append(" MsgTPL_LastLongitude llo,MsgTPL_LastLatitude lla,MsgTPL_EndTime ltime,ISNULL(MsgTPL_State,0) stat,MsgTPL_IsSpeed speed");
		sql.append(" from " + table);
		sql.append(" order by MsgTPL_Vcl_ID,MsgTPL_BeginTime");
		
		list = baseDAO.execQuery(sql.toString(), new Object[]{});
		
		return list;
	}

	/**
	 * 校验指定日期的“工作时间分段统计表”是否存在，如果不存在，则进行创建
	 **/
	public boolean checkCreateRoadInfo(String thisWorkDay) {
		boolean flag = false;
		String sql = "Select count(id) as counts FROM SysObjects Where name = 'Message_Stat_VclState_"+thisWorkDay+"' and xtype = 'U'";
		if(Integer.parseInt(MapGetter.getString(this.getBaseDAO().execQueryOne(sql, new Object[]{}),"counts"))>0){
			flag = true;
		}else{
			sql = "create table dbo.Message_Stat_VclState_"+thisWorkDay+"(MsgSVS_ID [decimal](18, 0) IDENTITY(1,1) NOT NULL,"
		        +"MsgSVS_VclID [varchar](20) NULL,"
				+"MsgSVS_BgnTime [datetime] NULL,"
		        +"MsgSVS_BLo [float] NULL,"
				+"MsgSVS_BLa [float] NULL,"
		        +"MsgSVS_EndTime [datetime] NULL,"
				+"MsgSVS_ELo [float] NULL,"
				+"MsgSVS_ELa [float] NULL,"
				+"MsgSVS_CndRunSec [int] NULL,"
				+"MsgSVS_State [int] NULL) ON [PRIMARY]";
			if(this.baseDAO.execUpdate(sql, new Object[]{})>-1){
				flag = true;
			}
		}
		return flag;	
	}
	
	public void insertDutyTimeSection(String thisWorkDay,List<Map<String, Object>> resultList,List<String> sqlList) {
		
		StringBuilder sql = new StringBuilder();
		Map<String,Object> mapRes = new HashMap<String, Object>();
		
		if(null != resultList && resultList.size() > 0){
			for(int i=0;i<resultList.size();i++){
				mapRes = resultList.get(i);
				
				if(0 != i && 0 != i/100){
					sqlList.add(sql.toString());
					sql = new StringBuilder();
				}
				long dtime = DateControl.getMiaoShu(MapGetter.getString(mapRes, "ftime"), MapGetter.getString(mapRes, "ltime"));
				
				sql.append("insert into Message_Stat_VclState_"+thisWorkDay);
				sql.append(" values('"+MapGetter.getString(mapRes, "vclid")+"','"+MapGetter.getString(mapRes, "ftime")+"','"+MapGetter.getString(mapRes, "flo")+"'");
				sql.append(" ,'"+MapGetter.getString(mapRes, "fla")+"','"+MapGetter.getString(mapRes, "ltime")+"','"+MapGetter.getString(mapRes, "llo")+"'");
				sql.append(" ,'"+MapGetter.getString(mapRes, "lla")+"','"+dtime+"','"+MapGetter.getString(mapRes, "stat")+"');");
				
				if(i == resultList.size()-1){
					sqlList.add(sql.toString());
				}
			}
		}
	}
	
	/**
	 * SQL集合批量执行
	 * @param sqlList
	 */
	public void executeSql(List<String> sqlList){
        List<String> list = new ArrayList<String>();
        MyConnectDSTransDAO ms=new MyConnectDSTransDAO();
        for(int i=0;i<sqlList.size();i++){
                list.add(sqlList.get(i));
                if((i%100==0 && i!=0) || (i == sqlList.size()-1)){
                        ms.batch(list);
                        list = new ArrayList<String>();
                }
        }
	}

	public int execInsert(String sql, Object[] objects) {
		// TODO Auto-generated method stub
		return baseDAO.execInsert(sql, objects);
	}
	
}
